package com.sy.controller;

import com.alibaba.excel.util.MapUtils;
import com.sy.auto.token.TokenTemplate;
import com.sy.bean.User;
import com.sy.bean.management.UpdatePass;
import com.sy.bean.management.UserLogin;
import com.sy.bean.management.UserStatus;
import com.sy.common.bean.PageBean;
import com.sy.common.bean.ResultBean;
import com.sy.common.controller.BaseController;
import com.sy.common.utils.BeanOfMap;
import com.sy.common.utils.DateUtils;
import com.sy.service.EnterpriseService;
import com.sy.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@RestController
@Api(tags = "用户")
@RequestMapping("/api")
public class UserController extends BaseController {
    @Autowired
    TokenTemplate tokenTemplate;
    @Autowired
    UserService userService;
    @Autowired
    EnterpriseService enterpriseService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public ResultBean login(@RequestBody UserLogin login, BindingResult result) {
        validate(result);
        User user = userService.queryByUsername(login.getUsername());
        if (null==user)
            return fail("账号不存在");
        //判断密码是否正确
        String pwd = DigestUtils.md5DigestAsHex(login.getPassword().getBytes());
        if (!pwd.equals(user.getPassword()))
            return fail("账号或密码不正确!");
        Map tmp = null;
        if (null!=tokenTemplate){
            tmp= BeanOfMap.bom(user);
            String token = tokenTemplate.generateToken(tmp);
            tmp.put("token",token);
        }
        //判断用户类型，如果是平台用户
        if (user.getType()==1)
            return success("登录成功",tmp);
        //如果是企业用户
        Map map = enterpriseService.queryByUserId(user.getId());
        if (null==map)
            return fail("请联系平台，完善企业信息!");
        //获取状态
        Integer status = BeanOfMap.getIntValue(map,"status");
        if (null==status||status==1)
            return fail("审核中");
        //获取到期日
        String expireDate = BeanOfMap.getStringValue(map,"expireDate");
        if (null==expireDate)
            return fail("请充值会员!");
        //获取系统时间
        String now = DateUtils.getYMD();
        if (expireDate.compareTo(now)<0)
            return fail("会员已过期!");
        tmp.put("eid",map.get("id"));
        tmp.put("ename",map.get("name"));
        tmp.put("expireDate",map.get("expireDate"));
        return success("登录成功",tmp);
    }
    @ApiOperation("账号状态")
    @PostMapping("/status")
    public ResultBean<UserStatus> queryStatus(@RequestBody UserStatus userStatus){
        UserStatus status = userService.queryStatus(userStatus);
        return success("账号状态",status);
    }
    @ApiOperation("修改密码")
    @PutMapping("/updatePass")
    public ResultBean<UpdatePass> updatePass(@RequestBody @Validated UpdatePass updatePass,BindingResult result){
        validate(result);
        int rs = userService.updatePass(updatePass);
        return rs==1?success("修改成功"):fail("旧密码不正确");
    }
    @ApiOperation("用户列表")
    @PostMapping("/user")
    public ResultBean<User> queryByType(@RequestBody User user){
        PageBean pageBean = userService.query(BeanOfMap.bom(user));
        return success("用户列表",pageBean);
    }
}
